import { RouteRecordRaw, Router, createRouter, createWebHistory } from "vue-router";
import Layout from '@/layout/index.vue';
import userinfo from "@/store/userinfo";


const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        component: Layout,
        redirect: '/home',
        children: [
            {
                path: '/login',
                name: "login",
                component: () => import('@/views/Login/index.vue')
            },
            {
                path: '/home',
                name: "Home",
                component: () => import('@/views/Home/index.vue')
            },
            {
                path: '/nagalandLottery',
                name: "nagalandLottery",
                component: () => import('@/views/NagalandLottery/index.vue')
            },
            {
                path: '/colorfultimes',
                name: "colorfultimes",
                component: () => import('@/views/Colorfultimes/index.vue')
            },
            {
                path: '/pick10Lottery',
                name: "pick10Lottery",
                component: () => import('@/views/pick10Lottery/index.vue')
            },
            {
                path: '/pick',
                name: "pick",
                component: () => import('@/views/Pick/index.vue')
            },
            {
                path: '/recommend',
                name: 'recommend',
                component: () => import('@/views/Recommend/index.vue'),
                // meta: { requiresAuth: true }
            },
            {
                path: '/recommend/inviteFriend',
                name: 'inviteFriend',
                component: () => import('@/views/Recommend/InviteFriend/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/result',
                name: 'result',
                component: () => import('@/views/Result/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/resultDetail',
                name: 'resultDetail',
                component: () => import('@/views/ResultDetail/index.vue')
            },
            {
                path: '/raceLottery',
                name: 'raceLottery',
                component: () => import('@/views/RaceLottery/index.vue'),
            },
            {
                path: '/race/:id',
                name: 'race',
                component: () => import('@/views/Race/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/redGreenLottery',
                name: 'redGreenLottery',
                component: () => import('@/views/RedGreenLottery/index.vue'),
            },
            {
                path: '/redGreen',
                name: 'redGreen',
                component: () => import('@/views/RedGreen/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/sixLottery',
                name: 'sixLottery',
                component: () => import('@/views/SixLottery/index.vue'),
            },
            {
                path: '/six',
                name: 'six',
                component: () => import('@/views/Six/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/diceLottery',
                name: 'diceLottery',
                component: () => import('@/views/DiceLottery/index.vue'),
            },
            {
                path: '/dice',
                name: 'dice',
                component: () => import('@/views/Dice/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/me',
                name: 'me',
                component: () => import('@/views/Me/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/me/account',
                name: 'account',
                component: () => import('@/views/Me/Account/index.vue')
            },
            {
                path: '/me/recharge',
                name: 'recharge',
                component: () => import('@/views/Me/Recharge/index.vue')
            },
            {
                path: '/me/recharge/records',
                name: 'rechargeRecords',
                component: () => import('@/views/Me/Recharge/Records/index.vue')
            },
            {
                path: '/me/withdraw',
                name: 'withdraw',
                component: () => import('@/views/Me/Withdraw/index.vue')
            },
            {
                path: '/me/withdraw/records',
                name: 'withdrawRecords',
                component: () => import('@/views/Me/Withdraw/Records/index.vue')
            },
            {
                path: '/me/withdraw/addbankcard',
                name: 'addbankcard',
                component: () => import('@/views/Me/Withdraw/AddBankCard/index.vue')
            },
            {
                path: '/me/invitations',
                name: 'invitations',
                component: () => import('@/views/Me/Invitations/index.vue')
            },
            {
                path: '/me/transactions',
                name: 'transactions',
                component: () => import('@/views/Me/Transactions/index.vue')
            },
            {
                path: '/me/changePassword',
                name: 'changePassword',
                component: () => import('@/views/Me/ChangePassword/index.vue')
            },
            {
                path: '/me/profile',
                name: 'profile',
                component: () => import('@/views/Me/Profile/index.vue')
            },
            {
                path: '/bet/:id',
                name: 'bet',
                component: () => import('@/views/Bet/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/colorfulLottery',
                name: 'colorfulLottery',
                component: () => import('@/views/ColorfulLottery/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/pick10',
                name: 'pick10',
                component: () => import('@/views/Pick10/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/viewPrivateOrder',
                name: 'viewPrivateOrder',
                component: () => import('@/views/ViewPrivateOrder/index.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: '/404',
                name: '404',
                component: () => import('@/views/404.vue'),
            },
            {
                path: '/:pathMatch(.*)',
                meta: {},
                redirect: '/404',
            },
        ]
    },
];

const router: Router = createRouter({
    history: createWebHistory(import.meta.env.VITE_BASE),
    routes,
    // 跳转路由后导航栏置顶
    scrollBehavior(to, from, saveScrollPosition) {
        return { left: 0, top: 0 };
    },
});

router.beforeEach((to, from, next) => {
    //进入页面时判断是否登录，如果未登录就跳到login页面，如果登录了就进入到相应的页面
    const piniaUserinfo: any = userinfo();
    const isLogin = piniaUserinfo.getHaslogged;
    const requiresAuth = to.matched.some(record =>record.meta.requiresAuth);
    console.log(requiresAuth, 'requiresAuth');
    if (requiresAuth && !isLogin) {
        next('/login');
    } else {
        next();
    }
});

export default router;
